## Chapter 1 (Introduction) 
## Our Common Bonds 
## Winter 2021 - 2022  

## Read in source file 
source("ocb_replication_file.r")


## For the ANES data, go to: https://www.electionstudies.org/ 


##############################
## NES Feeling Thermometers ##
############################## 

## Replace the data below with where you store it 
cdf <- read_dta(file="") 
nes2020 <- read_dta(file = "") 

## In the cumulative data, get the estimate of in-party and out-party ratings 
## for 2012 and 2016, do this separately by mode (FTF vs. Internet)
cdf <- cdf %>% 
  mutate(dem_ft = ifelse(VCF0218 < 98,VCF0218,NA),
         rep_ft = ifelse(VCF0224 < 98,VCF0224,NA),
         dem_cand_ft = ifelse(VCF0424 < 98, VCF0424, NA), 
         rep_cand_ft = ifelse(VCF0426 < 98, VCF0426, NA),
         pid = ifelse(VCF0301 > 0 & VCF0301 < 8, VCF0301, NA),
         dem = ifelse(pid<4,1,
                      ifelse(pid > 4,0,NA)), 
         same_ft = ifelse(pid < 4,dem_ft,
                          ifelse(pid > 4,rep_ft,NA)),
         other_ft = ifelse(pid > 4,dem_ft,
                           ifelse(pid < 4,rep_ft,NA)),
         ft_diff = same_ft - other_ft, 
         same_cand_ft = ifelse(pid < 4,dem_cand_ft,
                               ifelse(pid > 4,rep_cand_ft,NA)),
         other_cand_ft = ifelse(pid > 4,dem_cand_ft,
                                ifelse(pid < 4,rep_cand_ft,NA)),
         year = VCF0004, 
         ftf_interview = ifelse(VCF0017 == 0,1,
                                ifelse(VCF0017 == 4,0,NA))) 

## average FT rating (same party and other-party)
avg_fts <- cdf %>% 
  filter((year > 1977) & year != 2002 & !is.na(ftf_interview)) %>% 
  group_by(year,ftf_interview) %>% 
  summarise(avg_same = mean(same_ft, na.rm=T),
            avg_other = mean(other_ft, na.rm=T))

## Calculate in 2020 
nes2020 <- nes2020 %>% 
  mutate(dem_ft = ifelse(V201156 > -1 & V201156 < 101, V201156, NA), 
         rep_ft = ifelse(V201157 > -1 & V201157 < 101, V201157, NA), 
         dem_cand_ft = ifelse(V201151 > -1 & V201151 < 101, V201151, NA), 
         rep_cand_ft = ifelse(V201152 > -1 & V201152 < 101, V201152, NA), 
         pid = ifelse(V201231x > 0,V201231x,NA), 
         dem = ifelse(pid < 4,1,
                      ifelse(pid > 4,0,NA)), 
         same_ft = ifelse(pid < 4,dem_ft,
                          ifelse(pid > 4,rep_ft,NA)), 
         other_ft = ifelse(pid > 4,dem_ft,
                           ifelse(pid < 4,rep_ft,NA)), 
         same_cand_ft = ifelse(pid < 4,dem_cand_ft,
                               ifelse(pid > 4,rep_cand_ft,NA)), 
         other_cand_ft = ifelse(pid > 4,dem_cand_ft,
                                ifelse(pid < 4,rep_cand_ft,NA))) 

## means for this year 
mean(nes2020$same_ft, na.rm=T) 
mean(nes2020$other_ft, na.rm=T) 

## add these values in (need to ungroup/regroup to add row)
all_data <- avg_fts %>% 
  ungroup() %>%
  add_row(year = 2020, ftf_interview = 0, avg_same = 71.34, avg_other = 18.73) %>% 
  group_by(year, ftf_interview)


## group FT rating into one variable 
all_data <- all_data %>% 
  gather(key = label, 
         value = FT_Measure,
         c(avg_same,avg_other))

## for plotting, need a code that tells you same vs. other party & mode (ftf vs. internet)
all_data <- all_data %>% 
  mutate(target_mode = case_when(
    ftf_interview == 1 & label == "avg_same" ~ 1, 
    ftf_interview == 1 & label == "avg_other" ~ 2, 
    ftf_interview == 0 & label == "avg_same" ~ 3, 
    ftf_interview == 0 & label == "avg_other" ~ 4))  

## plot! 
pdf(file = "figures/chi_levendusky_fig01001.pdf",
    height = 11,
    width = 8.5)
all_data %>% 
  ggplot(aes(year,FT_Measure, 
             group = target_mode)) + 
  geom_line(aes(color = as.factor(target_mode),
                lty = as.factor(target_mode))) + 
  scale_color_manual(name = "",
                     values = c("black","darkgray","black","darkgray"), 
                     labels = c("Same-Party (FTF)","Other-Party (FTF)",
                                "Same-Party (Online)","Other-Party (Online)")) + 
  scale_linetype_manual(name = "",
                        values = c("solid","solid","dashed","dashed"), 
                        labels = c("Same-Party (FTF)","Other-Party (FTF)",
                                   "Same-Party (Online)","Other-Party (Online)")) + 
  scale_x_continuous(breaks = c(1980,1988,1996,2004,2012,2020)) + 
  ylim(c(0,100)) + 
  xlab("Year") + 
  ylab("Feeling Thermometer Rating") + 
  ggtitle("") +  
  theme_bw() + 
  #guides(color=guide_legend(nrow=2,byrow=TRUE))+
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off() 

## Look at Obama/Romney/Clinton/Trump/Biden ratings in 2012, 2016, and 2020
mean(cdf$same_cand_ft[cdf$pid < 4 & cdf$year == 2012 & cdf$ftf_interview == 0], na.rm=T) 
mean(cdf$same_cand_ft[cdf$pid > 4 & cdf$year == 2012 & cdf$ftf_interview == 0], na.rm=T) 
mean(cdf$same_cand_ft[cdf$pid < 4 & cdf$year == 2016 & cdf$ftf_interview == 0], na.rm=T) 
mean(nes2020$same_cand_ft[nes2020$pid < 4], na.rm=T) 
mean(cdf$same_cand_ft[cdf$pid > 4 & cdf$year == 2016 & cdf$ftf_interview == 0], na.rm=T) 
mean(nes2020$same_cand_ft[nes2020$pid > 4], na.rm=T) 
